<?php
//Name: util.php
//Purpose: This script is a place for misc functions used throughout the application.
//CreateDate: 10/01/12

// function loginUser($usernameToLogin, $passwordToLogin)
// {
// 	$returnValue = false;
// 	//to seperate each line in to an array entry
// 	$usersArray = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// 	//to seperate each user in to an array
// 	foreach ($usersArray as &$user)
// 	{
// 		$user = explode("|", $user);
// 	}

// 	//to match the username and password
// 	foreach($usersArray as $userExplode)
// 	{
// 		if($userExplode[0] == $usernameToLogin && $userExplode[1] == $passwordToLogin)
// 		{
			
// 			return $userExplode[0];
// 		}
// 	}

// 	header("location: /home.php");
// 	return null;
// }

function getMySqlConnection()
{
	$dbConnection = new mysqli("localhost","root","","tc_apparel");
	
	if(mysqli_connect_errno())
	{
		//an error in the connection occured
		printf("error");
		return false;
		exit;
	}

	return $dbConnection;
}

//MySQL
//returns the username if successful login
function loginUser($usernameToLogin, $passwordToLogin)
{

// 	$dbConnection = getMySqlConnection();
	
	$dbConnection = new mysqli("localhost","root","","tc_apparel");
	
	if($dbConnection == false)
	{
		header("location: /home.php");
	}
	
	$query = "SELECT * FROM person WHERE username='$usernameToLogin' AND password='$passwordToLogin';";
	
	$result = $dbConnection->query($query);
	
	if(!$result)
	{
		//cannot query the db at this time
		header("location: /home.php");
		return null;
		exit();
	}
	
	$numRows = $result->num_rows;
	
	if($numRows > 0) 
	{
		//success
		$row = $result->fetch_assoc();

		return $row["username"];
	}
	else 
	{
		//username or password is incorrect
		header("location: /home.php");
		return null;
	}
}

// function usernameIsAdmin($usernameIsAdmin)
// {
// 	$returnValue = false;
// 	//to seperate each line in to an array entry
// 	$usersArray = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// 	//to seperate each user in to an array
// 	foreach ($usersArray as &$user)
// 	{
// 		$user = explode("|", $user);
// 	}

// 	//to match the username and password
// 	foreach($usersArray as $userExplode)
// 	{
// 		if($userExplode[0] == $usernameIsAdmin)
// 		{
// 			//successful login
// 			session_name('login');
// 			session_start();
// 			if(end($userExplode) == "*")
// 			{
// 				$_SESSION['isAdmin'] = true;
// 			}
// 			session_write_close();
// 			$returnValue = true;
// 		}
// 	}
// 	return $returnValue;
// }

// function usernameIsAdmin($usernameIsAdmin)
// {
// 	$dbConnection = getMySqlConnection();
	
// 	$isAdminQuery = "SELECT *";
	
// 	$isAdminQueryResult = $dbConnection->query($isAdminQuery);
// }

function usernameExists($usernameToCheck)
{
		
// 	$dbConnection = getMySqlConnection();

	$dbConnection = new mysqli("localhost","root","","tc_apparel");
	
	if($dbConnection == false)
	{
		header("location: /home.php");
	}
	
	$query = "SELECT * FROM person WHERE username='$usernameToCheck';";
	
	$result = $dbConnection->query($query);
	
	if($result->num_rows > 0 )
	{
		return true;
	}
	return false;
	
}


// function usernameExists($usernameToCheck)
// {
// 	$returnValue = false;
// 	//to seperate each line in to an array entry
// 	$usersArray = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	
// 	//to seperate each user in to an array
// 	foreach ($usersArray as &$user)
// 	{
// 		$user = explode("|", $user);
// 	}
	
// 	//to match the username and password
// 	foreach($usersArray as $userExplode)
// 	{
// 		if($userExplode[0] == $usernameToCheck)
// 		{
// 			$returnValue = true;
// 		}
// 	}
	
// 	return $returnValue;
// }


?>